#include "IpEndPoint.hpp"

using namespace std;



Network::IpEndPoint::IpEndPoint(const IpAddress& ip, unsigned short port)
 : IpAddress(ip)
{
	SOCKADDR* addr = sockaddr();
	
	if(addr->sa_family == AF_INET)
		((SOCKADDR_IN*)addr)->sin_port = htons(port);
	
	else
		((SOCKADDR_IN6*)addr)->sin6_port = htons(port);
}



Network::IpEndPoint::IpEndPoint(const SOCKADDR* addr) throw(ArgumentException)
 : IpAddress(addr)
{
	
}



Network::IpEndPoint::~IpEndPoint()
{
    
}



unsigned short Network::IpEndPoint::port() const
{
    const SOCKADDR* addr = sockaddr();
	
	if(addr->sa_family == AF_INET)
		return ntohs(((SOCKADDR_IN*)addr)->sin_port);
	
	else
		return ntohs(((SOCKADDR_IN6*)addr)->sin6_port);
}



ostream& Network::operator<<(ostream& out, const Network::IpEndPoint& ep)
{
    out << ep.ip();
    return out;
}



